//
//  YZEHomeViewController.m
//  yilutongxing
//
//  Created by zjs on 17/3/10.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "UIImage+Layout.h"
#import "YZEHomeViewController.h"
#import "YZEHomeTableViewCell.h"
#import "RPUserInfoBean.h"
#import "YZENetwork.h"
#import "YZEHomeUtils.h"
#import "YZEHomeModel.h"
#import "YZEHomeClickViewController.h"
#import "SDCycleScrollView.h"
#import "SDCollectionViewCell.h"
#import "WebViewController.h"
#import "ZhuanXiangZhiBiaoViewController.h"

#define HomeURL @"/api/articles_hpone"

#define AddURL @"/api/articles_hpone?page="

@interface YZEHomeViewController ()<UITableViewDataSource,UITableViewDelegate,SDCycleScrollViewDelegate>

@property (nonatomic,strong)NSArray *sticksArray;
@property (nonatomic,strong)NSMutableArray *articlesArray;

@property (nonatomic,strong)NSMutableArray *IDArray;

@property (nonatomic,assign)NSInteger pagNum;
@end

@implementation YZEHomeViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
    [self initRefersh];

    self.IDArray = [NSMutableArray array];
    
    YZEHomeUtils *yzehu = [YZEHomeUtils new];
    [yzehu requestHomeDateWithDelegate:self AndUrl:[BASE stringByAppendingString:HomeURL] requestTag:1 Completion:^(id obj) {
        self.sticksArray = obj[@"sticks"];
        self.articlesArray = obj[@"articles"];
        //滚动视图web图片
        NSMutableArray *imageGroup = [NSMutableArray array];
        NSMutableArray *contentGroup = [NSMutableArray array];
        for (int i = 0; i<self.sticksArray.count; i++) {
            YZEHomeModel *m = self.sticksArray[i];
            NSString *imageStr = m.uri.absoluteString;
            [imageGroup addObject:imageStr];
            [contentGroup addObject:m.subtitle];
        }
        
        _m_tableHeadView.m_cycleScrollView.imageURLStringsGroup =imageGroup;
        _m_tableHeadView.m_cycleScrollView.titlesGroup=contentGroup;
        [self initRefersh];

        [self.m_tableView reloadData];
        
        //事先请求地区数据
        [[YZEModel sharedInstace]requestDistricts];
        //事先请求专项指标数据
        [ZhuanXiangZhiBiaoViewController requestIndicatorNameForCache];
    }];
    
}
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = NO;
}
-(void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    self.navigationController.navigationBar.hidden = NO;
    self.title = @"";
}
-(void)initUI{
    self.navigationItem.hidesBackButton=NO;
    
    _m_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT + 20) style:UITableViewStyleGrouped];
    if (@available(iOS 11.0, *)) {
        _m_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    _m_tableView.dataSource = self;
    _m_tableView.delegate = self;
    _m_tableView.backgroundColor = WHITE_COLOR;
    _m_tableView.showsVerticalScrollIndicator = NO;
    _m_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:_m_tableView];
    
    _m_tableHeadView = [[YZEHomeHeadView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, GTFixHeightFlaot(190))] ;
    _m_tableHeadView.m_cycleScrollView.delegate = self;
    
    _m_tableView.tableHeaderView = _m_tableHeadView;
}
-(void)initRefersh{

    __block YZEHomeViewController *homeVC = self;
    
    self.m_tableView.mj_header=[RefershHelper headerWithRefreshingBlock:^{
        YZEHomeUtils *yzehu = [YZEHomeUtils new];
        [yzehu requestHomeDateWithDelegate:self AndUrl:[BASE stringByAppendingString:HomeURL] requestTag:1 Completion:^(id obj) {
            self.sticksArray = obj[@"sticks"];
            self.articlesArray = obj[@"articles"];
            [self.m_tableView.mj_footer resetNoMoreData];
            [homeVC.m_tableView reloadData];
        }];
        NSLog(@"下拉刷新");
        [self.m_tableView.mj_header endRefreshing];
    }];
    self.pagNum = 0;
    self.m_tableView.mj_footer=[RefershHelper footerWithRefreshingBlock:^{
        homeVC.pagNum ++;
        YZEHomeUtils *add_yzehu = [YZEHomeUtils new];
        [add_yzehu requestHomeDateWithDelegate:self AndUrl:[NSString stringWithFormat:@"%@%@%ld",BASE,AddURL,(long)homeVC.pagNum] requestTag:1 Completion:^(id obj) {
            NSMutableArray *arr= [NSMutableArray arrayWithArray:obj[@"articles"]];
            if (arr.count>0) {
                [homeVC.articlesArray addObjectsFromArray:arr];
                [self.m_tableView reloadData];
            }
            else {
                [self.m_tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }];
        debugLog(@"homeVC.articlesArray.count=%lu",(unsigned long)homeVC.articlesArray.count);
        [self.m_tableView.mj_footer endRefreshing];
    }];

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.articlesArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 96;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YZEHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"home_view_cell"];
    if (!cell) {
        cell = [[YZEHomeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"home_view_cell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    YZEHomeModel *home=self.articlesArray[indexPath.row];
    cell.m_labelTitle.text = home.name;
    cell.m_labelContent.text = home.createdDate;
    [cell.m_imageview sd_setImageWithURL:home.uri completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        UIImage *new_image = [image scaleToSize:CGSizeMake(80, 60)];//等比缩放
        [cell.m_imageview setImage:new_image];
    }];
    [self.IDArray addObject:home.ID];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//     [[YZEModel sharedInstace] requestLoginWithUserName:nil password:nil delegate:self requestTag:2];

    //获取ID和Tltle
    NSString *ID = self.IDArray[indexPath.row];
    YZEHomeClickViewController *yzecVC = [YZEHomeClickViewController new];
    YZEHomeModel *home=self.articlesArray[indexPath.row];

    yzecVC.title = home.name;
    [yzecVC getWebID:ID];
    
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:yzecVC animated:YES];
}

//了解基金会按钮
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *backView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    backView.backgroundColor=Graycolor;
    UIButton* foundation_btn=[[UIButton alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, 40)];
    [foundation_btn setTitle:@"了解基金会" forState:UIControlStateNormal];
    foundation_btn.backgroundColor=[UIColor whiteColor];
    [foundation_btn setTitleColor:Bluecolor forState:UIControlStateNormal];
    [foundation_btn addTarget:self action:@selector(foundation_btnAction) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:foundation_btn];
    return backView;
}
-(void)foundation_btnAction{
    WebViewController *webVC = [WebViewController new];
    [self.navigationController pushViewController:webVC animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

#pragma mark 头部滑动图片
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
//    NSMutableArray *array=[[DBHelper getLKDBHelper] searchWithSQL:@"select * from @t" toClass:[RPUserInfoBean class]];
//    debugLog(@"%@",array);
    YZEHomeClickViewController *yzecVC = [YZEHomeClickViewController new];
    YZEHomeModel *home=self.sticksArray[index];
    yzecVC.title = home.subtitle;
    [yzecVC getWebID:home.ID];
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:yzecVC animated:YES];
}

#pragma mark 成功回调
-(void)OnSuccess:(id)responseObject requestTag:(int)tag{
    debugLog(@"[Login Log]%@",responseObject);
}

- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

@end
